﻿using System;
using System.Collections.Generic;
using System.Reflection;
using TK.Event;
using TK.SKill;
using UnityEditor;

namespace TK.Character
{
    /// <summary>
    ///  单位基类
    /// </summary>
    public class Unit
    {
        public Unit()
        {
        }

        /// <summary>
        /// 单位ID
        /// </summary>
        public int ID { get; set; }

        #region 生命部分
        /// <summary>
        ///  基础生命上限
        /// </summary>
        public float BaseHPLimit { get; set; }

        /// <summary>
        ///  额外生命上限
        /// </summary>
        public float ModifyHPLimit { get; set; }

        /// <summary>
        ///  总生命上限
        /// </summary>
        public float HPLimit => BaseHPLimit + ModifyHPLimit;

        /// <summary>
        /// 单位当前生命
        /// </summary>
        public float HP { get; set; }

        #endregion

        #region 护甲部分
        /// <summary>
        ///  单位基本护甲
        /// </summary>
        public float BaseArmor { get; set; }

        /// <summary>
        ///  单位额外护甲
        /// </summary>
        public float ModifyArmor { get; set; }

        /// <summary>
        ///  单位总护甲
        /// </summary>
        public float Armor => BaseArmor + ModifyArmor;

        #endregion

        #region 攻击力

        /// <summary>
        /// 单位的基础攻击力
        /// </summary>
        public float BaseDamage { get; set; }
        
        /// <summary>
        /// 额外攻击力
        /// </summary>
        public float ModifyDamage { get; set; }

        /// <summary>
        /// 总攻击力
        /// </summary>
        public float Damage => BaseDamage + ModifyDamage;

        #endregion

        #region 物理暴击

        /// <summary>
        /// 基础物理暴击
        /// </summary>
        public float BaseCriticalRate { get; set; }

        /// <summary>
        /// 额外物理暴击
        /// </summary>
        public float ModifyCriticalRate { get; set; }

        /// <summary>
        /// 总物理暴击率
        /// </summary>
        public float CriticalRate => BaseCriticalRate + ModifyCriticalRate;

        #endregion

        #region 闪避

        /// <summary>
        /// 基础闪避
        /// </summary>
        public float BaseDoge { get; set; }

        /// <summary>
        /// 额外闪避
        /// </summary>
        public float ModifyDoge { get; set; }

        /// <summary>
        /// 总闪避
        /// </summary>
        public float Doge => BaseDoge + ModifyDoge;

        #endregion

        #region 事件

        /// <summary>
        /// 造成伤害之前
        /// </summary>
        public BeforeApplyDamageEvent OnBeforeApplyDamage = null;

        /// <summary>
        /// 造成伤害的事件
        /// </summary>
        public ApplyDamageEvent OnApplyDamage = null;

        /// <summary>
        /// 造成伤害的事件
        /// </summary>
        public ApplyDamageEndEvent OnApplyDamageEnd = null;




        #endregion

        public void ApplyDamage(Unit target,float amount,DamageType type)
        {
            UnitDamageEventData ev = new UnitDamageEventData(target, this, amount, type);
            
            // 如果在事件里面返回了false，则不造成伤害
            if(OnBeforeApplyDamage != null && !OnBeforeApplyDamage.Invoke(ev)) return;
        }
    }
}

